﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace CommunitySystem.Assert
{
    public partial class MoveSwitch : UserControl
    {
        public event EventHandler MoveLeftCompleted;

        public event EventHandler MoveRightCompleted;

        public FrameworkElement LeftPage 
        {
            get { return placeHolder1.Children[0] as FrameworkElement; }
            set { placeHolder1.Children.Clear(); placeHolder1.Children.Add(value); } 
        }

        public FrameworkElement RightPage
        {
            get { return placeHolder2.Children[0] as FrameworkElement; }
            set { placeHolder2.Children.Clear(); placeHolder2.Children.Add(value); }
        }
        
        public MoveSwitch()
        {
            InitializeComponent();
        }

        public void MoveLeft()
        {
            daLeft1.To = -LayoutRoot.ActualWidth;
            daLeft2.From = LayoutRoot.ActualWidth + 4;
            sbMoveLeft.Begin();
            placeHolder2.Visibility = Visibility.Visible;
        }

        public void MoveRight()
        {
            daRight1.From = -LayoutRoot.ActualWidth - 4;
            daRight2.To = LayoutRoot.ActualWidth;
            sbMoveRight.Begin();
            placeHolder1.Visibility = Visibility.Visible;
        }

        private void LayoutRoot_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (e.NewSize.Height <= 0 || e.NewSize.Width <= 0)
            {
                return;
            }

            LayoutRoot.Clip = new RectangleGeometry() { Rect = new Rect(0, 0, e.NewSize.Width, e.NewSize.Height) };
        }

        private void sbMoveLeft_Completed(object sender, EventArgs e)
        {
            placeHolder1.Visibility = Visibility.Collapsed;

            if (MoveLeftCompleted != null)
            {
                MoveLeftCompleted(this, EventArgs.Empty);
            }
        }

        private void sbMoveRight_Completed(object sender, EventArgs e)
        {
            placeHolder2.Visibility = Visibility.Collapsed;

            if (MoveRightCompleted != null)
            {
                MoveRightCompleted(this, EventArgs.Empty);
            }
        }
    }
}
